package controllers.managers;

import java.io.IOException;
import java.util.Random;

import models.entity.Entity;
import models.entity.Troop;
import controllers.Game;

public class TroopGenerator implements Runnable{
	
	public Game game;
	private int numTroopP1, numTroopP2;
	
	public TroopGenerator(Game game){
		this.game = game;
		numTroopP1 = 0;
		numTroopP2 = 0;
		
		new Thread(this).start();
	}

	@Override
	public void run() {
		
		while(true){
			if(numTroopP1 > 0){
				addTroopP1ToTurn();
				numTroopP1--;
			}
			
			if(numTroopP2 > 0){
				addTroopP2ToTurn();
				numTroopP2--;
			}
			
			try {
				Thread.sleep(200);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

	private void addTroopP2ToTurn() {
	
		Troop troop = new Troop();
		troop.setWidth(game.stage.tilelong);
		
		int rnd2 = (int) Math.floor(Math.random()*(Game.HEIGHTSTAGE-game.stage.pixRestY));
		int aux2 = Game.HEIGHTSTAGE/game.stage.numYTiles;
		int l = 0;
		boolean end2 = false;
		
		while(l < Game.HEIGHTSTAGE-1 && !end2){
			if(rnd2>=0 && rnd2<aux2+l){
				troop.setPosY(l);
				troop.setPosYTile(l/aux2);
				end2 = true;
			}
			l += aux2;
		}
		
		troop.setPosX(game.stage.size-game.stage.tilelong-game.ind+2*game.stage.pixRestX);
		troop.setPosXTile(game.stage.numXTiles+4-1);
		troop.setPlayer(game.player2);
		troop.setCurrentlyStatus(0);
		
		Random rnd = new Random();
		int rndAux = rnd.nextInt(2);
		
		try {
			troop.addFrame("/images/entities/troop/movement/TroopMovementALeft.png", 0, 0);
			troop.addFrame("/images/entities/troop/movement/TroopMovementBLeft.png", 0, 1);
			troop.addFrame("/images/entities/troop/movement/TroopMovementCLeft.png", 0, 2);
			troop.addFrame("/images/entities/troop/movement/TroopMovementDLeft.png", 0, 3);
			troop.addFrame("/images/entities/troop/attack/TroopBlowLeftA.png", 1, 4);
			troop.addFrame("/images/entities/troop/attack/TroopBlowLeftB.png", 1, 5);
			troop.addFrame("/images/entities/troop/death/TroopDeathA1Left.png", 2, 6);
			
			if(rndAux==0){
				troop.addFrame("/images/entities/troop/death/TroopDeathB1Left.png", 2, 7);
				rndAux = rnd.nextInt(2);
				if(rndAux == 0){
					troop.addFrame("/images/entities/troop/death/TroopDeathC1Left.png", 2, 8);
				}else{
					troop.addFrame("/images/entities/troop/death/TroopDeathC2Left.png", 2, 8);
				}
				
			}else if(rndAux==1){
				troop.addFrame("/images/entities/troop/death/TroopDeathB2Left.png", 2, 7);
				rndAux = rnd.nextInt(3);
				if(rndAux == 0){
					troop.addFrame("/images/entities/troop/death/TroopDeathC3Left.png", 2, 8);
				}else if(rndAux == 1){
					troop.addFrame("/images/entities/troop/death/TroopDeathC4Left.png", 2, 8);
				}else{
					troop.addFrame("/images/entities/troop/death/TroopDeathC5Left.png", 2, 8);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		game.player2.addEntity((Entity)troop);
		game.entities.add((Entity)troop);
			
	}

	private void addTroopP1ToTurn() {
		
		Troop troop = new Troop();
		troop.setWidth(game.stage.tilelong);
		
		int rnd1 = (int) Math.floor(Math.random()*((Game.HEIGHTSTAGE)-game.stage.pixRestY));
		int aux1 = Game.HEIGHTSTAGE/game.stage.numYTiles;
		int j = 0;
		boolean end1 = false;
		while(j < Game.HEIGHTSTAGE-1 && !end1){
			if(rnd1>=0 && rnd1<aux1+j){
				troop.setPosY(j);
				troop.setPosYTile(j/aux1);
				end1 = true;
			}
			j += aux1;
		}
		
		troop.setPosX(0-game.ind);
		troop.setPosXTile(-2);
		troop.setPlayer(game.player1);
		troop.setCurrentlyStatus(0);
		
		Random rnd = new Random();
		int rndAux = rnd.nextInt(2);
		
		try {
			troop.addFrame("/images/entities/troop/movement/TroopMovementARight.png", 0, 0);
			troop.addFrame("/images/entities/troop/movement/TroopMovementBRight.png", 0, 1);
			troop.addFrame("/images/entities/troop/movement/TroopMovementCRight.png", 0, 2);
			troop.addFrame("/images/entities/troop/movement/TroopMovementDRight.png", 0, 3);
			troop.addFrame("/images/entities/troop/attack/TroopBlowRightA.png", 1, 4);
			troop.addFrame("/images/entities/troop/attack/TroopBlowRightB.png", 1, 5);
			troop.addFrame("/images/entities/troop/death/TroopDeathA1Right.png", 2, 6);
			
			if(rndAux==0){
				troop.addFrame("/images/entities/troop/death/TroopDeathB1Right.png", 2, 7);
				rndAux = rnd.nextInt(2);
				if(rndAux == 0){
					troop.addFrame("/images/entities/troop/death/TroopDeathC1Right.png", 2, 8);
				}else{
					troop.addFrame("/images/entities/troop/death/TroopDeathC2Right.png", 2, 8);
				}
				
			}else if(rndAux==1){
				troop.addFrame("/images/entities/troop/death/TroopDeathB2Right.png", 2, 7);
				rndAux = rnd.nextInt(3);
				if(rndAux == 0){
					troop.addFrame("/images/entities/troop/death/TroopDeathC3Right.png", 2, 8);
				}else if(rndAux == 1){
					troop.addFrame("/images/entities/troop/death/TroopDeathC4Right.png", 2, 8);
				}else{
					troop.addFrame("/images/entities/troop/death/TroopDeathC5Right.png", 2, 8);
				}
			}
			
		} catch (IOException e){
			e.printStackTrace();
		}
		
		game.player1.addEntity((Entity)troop);
		game.entities.add((Entity)troop);
				
	}

	public void addTroopsToTurn() {
		numTroopP1 = game.player1.numTroops;
		numTroopP2 = game.player2.numTroops;
	}
}
